import {MapData} from "@/class/map/MapData";
import {getBounds} from "@/utils/map";
import {convert_gcj02_To_Gps84, convert_gps84_To_Gcj02, MyMapOpt} from "@/class/map/myMapOpt";
import {reqConvertGps84ToGcj02, reqDeleteMapData} from "@/api/mapdata";
import {reqPostPolyline, reqPutPolyline} from "@/api/polyline"
import {requestGetData, requestIsSuccess} from "@/api/request";
import {getRandomColor, getUUID, toJson} from "@/utils/utils";

export class PolylineData{
    constructor({mapData, polyline}) {
        this.mapData=new MapData(mapData)
        this.polyline=new Polyline(polyline)
    }
    /**
     * 移动到该要素
     * @param map
     */
    moveTo(map){
        const element = this.getDateInMap(map)
        var bounds = element.getBounds();
        map.setBounds(bounds)
        this.blinkData(map)
        /*const path = this.polyline.path
        const getBound = getBounds();
        const result =getBound(path);
        const bounds = new AMap.Bounds(result.southWest,result.northEast);
        map.setBounds(bounds,false,[0,0,0,0])*/
    }
    /**
     * 添加要素，并返回地图要素
     * @param map
     * @returns {*}
     */
    addToMap(map,mapCoordSys) {
        if(this.mapData.id===undefined){
            this.mapData.id=getUUID();
        }
        if(this.polyline.id===undefined){
            this.polyline.id=getUUID();
        }const result = this.getBaiduMapOpt(mapCoordSys)
        const feature = new AMap[MyMapOpt.MapType.Polyline](result);
        feature.setExtData({
            id: this.mapData.id,
        })
        map.add(feature)
        return feature
    }

    /**
     * 返回地图需要的数据，如果坐标不是CJ02，转换为CJ02
     * @returns {Promise<any>}
     */
    getBaiduMapOpt(mapCoordSys) {
        const opt = JSON.parse(JSON.stringify(this.polyline))
        //坐标转换
        switch (mapCoordSys){
            case MyMapOpt.MapCoordSys.CJ02:
                if(this.mapData.coordsys!==MyMapOpt.MapCoordSys.CJ02)
                    opt.path=convert_gps84_To_Gcj02(opt.path)
                break;
            case MyMapOpt.MapCoordSys.GPS:
                if(this.mapData.coordsys!==MyMapOpt.MapCoordSys.GPS)
                    opt.path=convert_gcj02_To_Gps84(opt.path)
                break;
        }
        return opt
    }


    /**
     * 更新面数据
     * @returns true:更新成功;false:更新失败
     */
    async update() {
        const result =  await reqPutPolyline(this)
        return requestIsSuccess(result)
    }
    /**
     * 添加面数据
     * @returns true:更新成功;false:更新失败
     */
    async upload() {
        const result = await reqPostPolyline(this)
        return requestIsSuccess(result)
    }
    /**
     * 返回要素的原始设置
     * @returns {Marker}
     */
    getElement(){
        return this.polyline
    }
    /**
     * 判断能否正确添加到该地图
     * @returns {boolean}
     */
    isElement(){
        return this.polyline.path.length>0
    }
    /**
     * 返回复制对象
     * @returns {MarkerData}
     */
    getCopy(){
        return new PolylineData(this)
    }

    /**
     * 返回地图中的对象
     * @param map
     * @returns {*}
     */
    getDateInMap(map){
        return map.getAllOverlays().find(item=>{
            return item.getOptions().id===this.polyline.id
        })
    }
    /**
     * 删除map中的元素
     * @param map
     */
    deleteDataInMap(map){
        map.remove(this.getDateInMap(map))
    }
    /**
     * 闪烁该要素
     * @param map
     */
    blinkData(map){
        var mapData = this.getDateInMap(map);
        //闪烁次数
        let blinkTimes=6
        let currentTimes=0
        //当前透明度
        const strokeOpacity=mapData.getOptions().strokeOpacity
        const interval = setInterval(()=>{
            if(currentTimes<blinkTimes){
                if(mapData.getOptions().strokeOpacity>0.5){
                    mapData.setOptions({
                        "strokeOpacity":0
                    })
                }else{
                    mapData.setOptions({
                        "strokeOpacity":1
                    })
                }
                currentTimes++
            }else{
                clearInterval(interval)
                mapData.setOptions({
                    "strokeOpacity":strokeOpacity
                })
            }
        },100)
    }
    /**
     * 下载json数据
     */
    getJSON(){
        toJson(this.mapData.name,this)
    }
    /**
     * 删除要素
     */
    deleteElement(){
        return reqDeleteMapData(this.mapData.id)
    }
}
export class Polyline{
    constructor({id, strokeColor, path, extData}) {
        this.id=id
        this.strokeColor=strokeColor||getRandomColor()
        this.path=path
        this.extData=extData
    }
}